package terracraft;

import java.io.IOException;
import java.io.InputStream;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

import terracraft.blocs.Bloc;

/**
 * Classe principale du jeu.
 * 
 * @author linkpy
 */
public class Terracraft extends BasicGame {
    /** Conteneur pour la classe Terracraft */
    public static AppGameContainer Appli;
    
    /**
     * Charge l'image à l'adresse PATH dans l'archive JAR.
     * 
     * @param path L'adresse de l'image dans l'archive JAR.
     * @return L'image chargé.
     */
    public static Image chargeImage(String path) {
        System.out.println("Chargement de l'image "+path+" ...");
        try {
            InputStream in = Terracraft.class.getClassLoader().
                    getResourceAsStream(path);
            
            if(in == null)
                throw new GameError("Terracraft.chargeImage", "Impossible "
                        + "d'ouvire le fichier "+path);
            
            Image i = new Image(in, path, false);
            i.setFilter(Image.FILTER_NEAREST);
            in.close();
            
            return i;
        } catch (SlickException ex) {
            throw new GameError("Terracraft.chargeImage", "Erreur lors du "
                    + "chargement de l'image "+path, ex);
        } catch(IOException ex) {
            throw new GameError("Terracraft.chargeImage", "Erreur lors du "
                    + "chargement de l'image "+path, ex);
        }
    }
    
    public static void main(String[] args) {
        try {
            Appli = new AppGameContainer(new Terracraft());
            Appli.setDisplayMode(800, 600, false);
            Appli.start();
        } catch (SlickException ex) {
            System.err.println(" Erreur: "+ex.getMessage());
            ex.printStackTrace(System.err);
            System.exit(1);
        }
    }

    public Terracraft() {
        super("Terracraft - 0.0.0");
    }
    
    @Override
    public void init(GameContainer container) 
            throws SlickException {
    }

    @Override
    public void update(GameContainer container, int delta) 
            throws SlickException {
    }

    @Override
    public void render(GameContainer container, Graphics g) 
            throws SlickException {
        g.pushTransform();
        Bloc.air.afficher(100, 100, g);
        g.popTransform();
        
        g.pushTransform();
        Bloc.terre.afficher(108, 100, g);
        g.popTransform();
        
        g.pushTransform();
        Bloc.herbe.afficher(116, 100, g);
        g.popTransform();
    }
}
